#include <stdio.h>
#include <string.h>
#include "Qstack.h"
static QstackCallbackStructType Mux;
static int QstackBuffCheck(QstackStructType *cb)
{
    if (cb->head == 0)
        return 0;
    else if (cb->head == QSTACKBUFF_SIZE)
        return 1;
    else
        return 2;
}

QStack_enumType QstackInit(QstackStructType *cb)
{
    cb->head = 0;
    memset(cb->buff, 0, QSTACKBUFF_SIZE);
    return QStackOk;
}
QStack_enumType QstackRegister(void (*lock)(int), void (*ulock)(int))
{
    Mux.QstackCbLock = lock;
    Mux.QstackCbULock = ulock;
    if (Mux.QstackCbLock != NULL && Mux.QstackCbULock != NULL)
        return QStackOk;
    else
        return QStackLockRegisterError;
}
QStack_enumType QstackWrite(QstackStructType *cb, char data, int lockNum)
{
    Mux.QstackCbLock(lockNum);
    if (QstackBuffCheck(cb) == 1)
    {
        printf("栈满\n");
        Mux.QstackCbULock(lockNum);
        return QStackFull;
    }
    else
    {
        cb->buff[cb->head] = data;
        cb->head++;
        Mux.QstackCbULock(lockNum);
        return QStackOk;
    }
}
QStack_enumType QstackRead(QstackStructType *cb, char *data, int lockNum)
{

    Mux.QstackCbLock(lockNum);
    if (QstackBuffCheck(cb) == 0)
    {
        printf("栈空\n");
        *data = 'N';
        Mux.QstackCbULock(lockNum);
        return QStackNull;

    }
    else
    {
        cb->head--;
        *data = cb->buff[cb->head];
        Mux.QstackCbULock(lockNum);
        return QStackOk;
    }
}